package com.ctshk.rpc.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.bus.PassengerType;
import com.ctshk.common.utils.Assist;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.bus.dto.BusProductCostDTO;
import com.ctshk.rpc.bus.dto.BusProductCostPriceDTO;
import com.ctshk.rpc.bus.entity.BusProductCost;
import com.ctshk.rpc.bus.entity.BusProductCostPrice;
import com.ctshk.rpc.bus.mapper.BusProductCostMapper;
import com.ctshk.rpc.bus.service.IBusProductCostPriceService;
import com.ctshk.rpc.bus.service.IBusProductCostService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 巴士产品成本表 服务实现类
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-02-19
 */
@DubboService
public class BusProductCostServiceImpl extends ServiceImpl<BusProductCostMapper, BusProductCost> implements IBusProductCostService {

    @Autowired
    BusProductCostMapper busProductCostMapper;

    @DubboReference
    IBusProductCostPriceService busProductCostPriceService;

    /**
     * 查询巴士产品成本
     *
     * @param ids
     * @return
     */
    @Override
    public List<BusProductCostDTO> findBusCostList(List<Long> ids) {
        if(ids == null || ids.size() <= 0 ){
            return null;
        }

        List<BusProductCost> costList = busProductCostMapper.selectList(Wrappers.<BusProductCost>lambdaQuery().select(BusProductCost::getId, BusProductCost::getBusProductId).in(ids != null && ids.size() > 0, BusProductCost::getBusProductId, ids));
        List<BusProductCostDTO> busProductCostDTOS = EntityUtil.copyList(costList, BusProductCostDTO.class);

        // 查询巴士成本详情
        if (costList != null && costList.size() > 0) {
            // 收集成本id
            List<Long> costIds = costList.stream().map(item -> item.getId()).collect(Collectors.toList());
            // 查询成本详情
            List<BusProductCostPriceDTO> busCostPrices = busProductCostPriceService.findBusCostPriceList(costIds);
            Assist.forEach(busProductCostDTOS, cost -> {
                // 设置成本详情列表
                cost.setBusProductCostPriceDtoList(busCostPrices.stream().filter(item -> item.getCostId().equals(cost.getId())).collect(Collectors.toList()));
            });
        }

        return busProductCostDTOS;
    }
}
